<?php
	use\ampleharvest\code\cConfig;
	/**
	 * @package AmpleHarvest
	 */
	require_once dirname(dirname(__FILE__)) . '/classes/cAmpleHarvestLog.php';
	require_once dirname(dirname(__FILE__)) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	require_once $codeConfig->C_STR_STOMP_LIB_PATH . "/Stomp.php";
	/**
	 * @brief This class is used to interact with a stomp message queue
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 29 JUN 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes
	 */
	class cStomp
	{
		const C_STR_QUEUE_EMAIL = "/queue/email";
		const C_STR_QUEUE_SOLR_DELETE = "/queue/solr.delete";
		const C_STR_QUEUE_AH_NEW = "/queue/ah.new";
		const C_STR_QUEUE_AH_ERROR = "/queue/ah.error";
		const C_STR_QUEUE_SOLR_NEW = "/queue/solr.new";
		const C_STR_QUEUE_REAPER_NEW = "/queue/reaper.new";
		const C_STR_QUEUE_GE_NEW = "/queue/ge.new";
		const C_STR_QUEUE_GS_NEW = "/queue/gs.new";

		private $m_obj_Connection = null;

		private $m_obj_Log = null;

		public function __construct()
		{
			$this->m_obj_Log = new \cAmpleHarvestLog();
			$str_Error = "";
			try
			{
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::c_str_ACTION_VALUE,
					"Server = " . cConfig::C_STR_STOMP_SERVER,
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::c_str_ACTION_VALUE,
					"Port = " . cConfig::C_STR_STOMP_PORT,
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);

				$this->m_obj_Connection = new Stomp("tcp://" . cConfig::C_STR_STOMP_SERVER . ":" . cConfig::C_STR_STOMP_PORT);

				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_INFO,
					"Successfuly Created STOMP Server.",
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			}
			catch (StompException $e)
			{
				error_log("Failed to create STOMP Server." . $e->getMessage());
				
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_ERROR,
					"Failed to create STOMP server.\n " . $e->getMessage(),
					\cAmpleHarvestLog::C_INT_LEVEL_ERRORS);

				trigger_error("Failed to create STOMP server.\n " . $e->getMessage(), E_USER_WARNING);
				return;
			}

			try
			{
				$this->m_obj_Connection->connect();
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_INFO,
					"Successfuly Connected to STOMP Server.",
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			}
			catch (StompException $e)
			{
				error_log("Failed to connect to STOMP server.\n  " . $e->getMessage());
				
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_ERROR,
					"Failed to connect to STOMP server.\n " . $e->getMessage(),
					\cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
				throw new Exception("Failed to connect to STOMP server.\n " . $e->getMessage());
				return;
			}

			$this->m_obj_Connection->setReadTimeout(10);
		}

		public function __destruct()
		{
			if (!$this->m_obj_Connection)
			{
				$this->m_obj_Connection->disconnect();
			}
		}

		public function send($str_Queue, $str_Data, $int_Priority = 5, $bool_Persistent = true)
		{
			if (!$this->m_obj_Connection)
			{
				return false;
			}
			try
			{
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::c_str_ACTION_VALUE,
					"Queue = " . $str_Queue,
					\cAmpleHarvestLog::C_INT_LEVEL_HIDDEN);
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::c_str_ACTION_VALUE,
					"Data = " . $str_Data,
					\cAmpleHarvestLog::C_INT_LEVEL_HIDDEN);

				$arr_Headers = array();

				$arr_Headers["priority"] = $int_Priority;

				if ($bool_Persistent)
				{
					$arr_Headers["persistent"] = 'true';
				}
				else
				{
					$arr_Headers["persistent"] = 'false';
				}

				$this->m_obj_Connection->send($str_Queue, $str_Data, $arr_Headers);

				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_INFO,
					"Successfuly send message to STOMP Server.",
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			}
			catch (StompException $e)
			{
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_ERROR,
					"Failed to send message to STOMP server.\n " . $e->getMessage(),
					\cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
				return false;
			}
			return true;
		}

		public function subscribe($str_Queue)
		{
			if (!$this->m_obj_Connection)
			{
				return false;
			}
			try
			{
				$this->m_obj_Connection->subscribe($str_Queue);
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_INFO,
					"Successfuly subscribed to STOMP queue.",
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			}
			catch (StompException $e)
			{
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_ERROR,
					"Failed to subscribed to STOMP queue.\n " . $e->getMessage(),
					\cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
				return false;
			}
			return true;
		}

		public function getMessage()
		{
			$str_Msg = "";
			if (!$this->m_obj_Connection)
			{
				return false;
			}
			try
			{
				$str_Msg = $this->m_obj_Connection->readFrame();
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_INFO,
					"Successfuly read message from STOMP queue.",
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			}
			catch (StompException $e)
			{
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_ERROR,
					"Failed to read message from STOMP queue.\n " . $e->getMessage(),
					\cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
				return false;
			}
			return $str_Msg;
		}

		public function ackMessage($obj_Msg)
		{
			if (!$this->m_obj_Connection)
			{
				return false;
			}
			try
			{
				$this->m_obj_Connection->ack($obj_Msg);
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_INFO,
					"Successfuly acknolaged message from STOMP queue.",
					\cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			}
			catch (StompException $e)
			{
				$this->m_obj_Log->do_Log(\cAmpleHarvestLog::C_STR_ACTION_STOMP_ERROR,
					"Failed to acknolaged message from STOMP queue.\n " . $e->getMessage(),
					\cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
				return false;
			}
			return true;
		}

		public static function get_SolrIndexQueue($str_Client = null)
		{
			if (!$str_Client)
			{
				return cConfig::C_STR_SOLR_QUEUE;
			}
			$str_Client = strtoupper($str_Client);
			switch ($str_Client)
			{
				case "GENEERALSCARE": //TODO is the client sent here misspelled as well?
					return self::C_STR_QUEUE_GS_NEW;
				case "GROUNDEFFECT":
				case "FTP":
				case "NEW_GROUNDEFFECT":
					return self::C_STR_QUEUE_GE_NEW;
				default:
					return self::C_STR_QUEUE_REAPER_NEW;
			}
		}
	}
